package com.zhiyou100.controller;

import com.zhiyou100.model.Order;
import com.zhiyou100.service.OrderService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 导出Excel表格
 * 2020年03月22日17:56:39
 */

@Controller
@RequestMapping("/poi")
public class POIController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/export")
    public void exportExcel(HttpServletResponse resp) throws IOException {
        Map<String,String> map = new HashMap<String,String>();
        // 调用业务层,查出全部信息
        List<Order> orders = orderService.listOrder(map);
        // 1创建Excel
        HSSFWorkbook wb = new HSSFWorkbook();
        // 2创建Sheet页面
        HSSFSheet sheet = wb.createSheet("订单信息");
        // 3创建第一行,表头行
        HSSFRow r1 = sheet.createRow(0);
        // 4.创建所需列
        HSSFCell r1c1 = r1.createCell(0);
        HSSFCell r1c2 = r1.createCell(1);
        HSSFCell r1c3 = r1.createCell(2);
        HSSFCell r1c4 = r1.createCell(3);
        HSSFCell r1c5 = r1.createCell(4);
        HSSFCell r1c6 = r1.createCell(5);
        HSSFCell r1c7 = r1.createCell(6);
        HSSFCell r1c8 = r1.createCell(7);
        r1c1.setCellValue("客户姓名");
        r1c2.setCellValue("房间号");
        r1c3.setCellValue("押金");
        r1c4.setCellValue("是否退还押金");
        r1c5.setCellValue("住宿时长");
        r1c6.setCellValue("订单创建时间");
        r1c7.setCellValue("客户退房时间");
        r1c8.setCellValue("费用");
        // 遍历集合,确定集合有几个元素,创建几行
        for (int i = 0; i < orders.size(); i++) {
            // 新创建一行
            HSSFRow row = sheet.createRow(i + 1);
            // 在行内创建四列
            HSSFCell c1 = row.createCell(0);
            HSSFCell c2 = row.createCell(1);
            HSSFCell c3 = row.createCell(2);
            HSSFCell c4 = row.createCell(3);
            HSSFCell c5 = row.createCell(4);
            HSSFCell c6 = row.createCell(5);
            HSSFCell c7 = row.createCell(6);
            HSSFCell c8 = row.createCell(7);
            c1.setCellValue(orders.get(i).getOrderName());
            c2.setCellValue(orders.get(i).getOrderRoomId());
            c3.setCellValue(orders.get(i).getOrderCash());
            c4.setCellValue(orders.get(i).getOrderCashback());
            c5.setCellValue(orders.get(i).getOrderTime());
            c6.setCellValue(orders.get(i).getOrderCreateTime());
            c7.setCellValue(orders.get(i).getOrderEndTime());
            c8.setCellValue(orders.get(i).getOrderMoney());
        }
        // 解决响应中文文件名乱码问题
        String filename = URLEncoder.encode("订单信息表", "utf-8");
        // 浏览器响应下载弹框
        resp.setHeader("Content-disposition", "attachment;filename=" + filename + ".xls");
        resp.setContentType("application/msexcel");
        // 输出
        OutputStream out = resp.getOutputStream();
        wb.write(out);
        out.close();
    }



}
